{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   }
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from mpl_toolkits.mplot3d import Axes3D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.linspace(-1,1,100)\n",
    "y1 = 2*x + 1\n",
    "y2 = x**2\n",
    "plt.figure()   # 重新弄一个画图对象\n",
    "plt.xlim((-1,2)) #xy 坐标范围\n",
    "plt.ylim((-2,3))\n",
    "plt.xlabel('I AM X') #xy范围\n",
    "plt.ylabel('I AM Y')\n",
    "l1,=plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')#虚线\n",
    "# plt.figure()  # 重新弄一个画图对象\n",
    "l2,=plt.plot(x,y2,color='blue',linewidth=5.0,linestyle='-',figsize=(8,5)) # linestyle实线 figsize 大小\n",
    "plt.xticks(p.linspace(-2,2,11)) # 坐标点重新描述\n",
    "plt.yticks([-1,0,1,2,3],\n",
    "           ['level1','level2','level3','level4','level5']) \n",
    "ax = plt.gca()  # 得到坐标描述对象\n",
    "ax.spines['right'].set_color('none') #把右边和上边的边框去掉\n",
    "ax.spines['top'].set_color('none')\n",
    "ax.xaxis.set_ticks_position('bottom')#把x轴的刻度设置为‘bottom’\n",
    "ax.yaxis.set_ticks_position('left')#把y轴的刻度设置为‘left’\n",
    "ax.spines['bottom'].set_position(('data',0)) #设置bottom对应到0点\n",
    "ax.spines['left'].set_position(('data',0))#设置left对应到0点\n",
    "plt.legend(handles=[l1,l2],labels=['test1','test2'],loc='best') #图例描述\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x0 = 0.5\n",
    "y0 = 2*x0 + 1\n",
    "#画点\n",
    "plt.scatter(x0,y0,s=50,color='b')\n",
    "#画虚线\n",
    "plt.plot([x0,x0],[y0,0],'k--',lw=2)\n",
    "\n",
    "plt.annotate(r'$2x+1=%s$' % y0,xy=(x0,y0),xytext=(+30,-30),textcoords='offset points',fontsize=16,\n",
    "             arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2'))\n",
    "\n",
    "plt.text(-1,2,r'$this\\ is\\ the\\ text$',fontdict={'size':'16','color':'r'}) # 生成标注"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.random.normal(0,1,500)\n",
    "y = np.random.normal(0,1,500)\n",
    "plt.scatter(x,y,s=50,c='b',alpha=0.5) # 散点图 alpha：透明度\n",
    "plt.xlim((-2,2))\n",
    "plt.ylim((-2,2))\n",
    "plt.xticks(())\n",
    "plt.yticks(())\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.arange(10)\n",
    "y = 2**x + 10\n",
    "plt.bar(x,y,facecolor='#9999ff',edgecolor='white') # 直方图 \n",
    "for x,y in zip(x,y):\n",
    "    plt.text(x+0.4,y,'%.2f' % y,ha='center',va='bottom')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x, y):  \n",
    "    return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) \n",
    "\n",
    "x = np.linspace(-3,3,100)\n",
    "y = np.linspace(-3,3,100)\n",
    "\n",
    "X,Y = np.meshgrid(x,y) # 生成网格点坐标矩阵 用于等高线、）SVC中超平面的绘制：\n",
    "plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot)\n",
    "\n",
    "C = plt.contour(X,Y,f(X,Y),8,colors='black',linewidth=.5)\n",
    "plt.clabel(C,inline=True,fontsize=10) # 等高线\n",
    "\n",
    "plt.xticks(())\n",
    "plt.yticks(())\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "ax = Axes3D(fig)\n",
    "\n",
    "x = np.arange(-4,4,0.25)\n",
    "y = np.arange(-4,4,0.25)\n",
    "X,Y = np.meshgrid(x,y)\n",
    "R = np.sqrt(X**2 + Y**2)\n",
    "Z = np.sin(R)\n",
    "\n",
    "ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))\n",
    "ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow')\n",
    "ax.set_zlim(-2,2)\n",
    "\n",
    "plt.show() # 3D 画图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.subplot(2,1,1)\n",
    "plt.plot([0,1],[0,1])\n",
    "\n",
    "plt.subplot(2,3,4)\n",
    "plt.plot([0,1],[0,1])\n",
    "\n",
    "plt.subplot(235)\n",
    "plt.plot([0,1],[0,1])\n",
    "\n",
    "plt.subplot(236)\n",
    "plt.plot([0,1],[0,1])\n",
    "\n",
    "plt.show()#子图绘制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import animation\n",
    "fig,ax = plt.subplots()\n",
    "\n",
    "x = np.arange(0,2*np.pi,0.01)\n",
    "line, = ax.plot(x,np.sin(x))\n",
    "\n",
    "def animate(i):\n",
    "    line.set_ydata(np.sin(x+i/10))\n",
    "    return line,\n",
    "\n",
    "def init():\n",
    "    line.set_ydata(np.sin(x))\n",
    "    return line,\n",
    "\n",
    "ani = animation.FuncAnimation(fig=fig,func=animate,init_func=init,interval=20)\n",
    "plt.show() # 绘制动态图"
   ]
  }
 ]
}